package com.liangzhong.model.leagueMatch.vo
{
	import flash.events.Event;
	import flash.events.EventDispatcher;

	/**
	 *甲级联赛信息vo 
	 * @author lake
	 * 
	 */	
	public class BaseMatchInfoVo extends EventDispatcher
	{
		/**
		 *打到的比赛index 
		 */		
		public var matchindex		:int;
		/**
		 *获胜场次
		 */		
		public var win				:int;
		/**
		 * 比赛结果信息
		 * {hgoal:1,ggoal:2}
		 * 主场进球数  客场进球数
		 */		
		public var matchResList	:Array = [];
		/**
		 *俱乐部关系列表 
		 * {npcid:1,relation:20,starid1:0,starid2:0,starid3:0}
		 */		
		public var clubrelationList :Array = [];
		
		public function BaseMatchInfoVo()
		{
		}
		/**
		 *解析 
		 * @param data
		 * 
		 */		
		public function parse(data:Object):void
		{
			matchindex = data.matchindex;
			win = data.win;
			var obj :Object ;
			matchResList = [];
			for(var i:int = 1;i <= 11;i++)
			{
				obj = {};
				obj.hgoal = data["hgoal" + i];
				obj.ggoal = data["ggoal" + i];
				matchResList.push(obj);
			}
			this.dispatchEvent(new Event(Event.CHANGE));
		}
		/**
		 *更新比赛结果 
		 * @param data
		 * 
		 */		
		public function updateResult(data :Object):void
		{
			var index :int = data.index;
			if(index >= matchResList.length)
			{
				return ;
			}
			var obj :Object = matchResList[index];
			obj.hgoal = data.hgoal;
			obj.ggoal = data.ggoal;
			if(obj.hgoal > obj.ggoal)
			{
				win ++;
			}
			matchindex = index + 1;
			var npcid:int = data.npcid;
			var relObj :* = getClubRelationById(npcid);
			relObj.relation = (int(relObj.relation) + 1)>99?99:(int(relObj.relation) + 1);
			if(matchindex < 11)
			{
				this.dispatchEvent(new Event(Event.CHANGE));
			}
		}
		/**
		 *设置俱乐部关系表 
		 * @param data
		 * 
		 */		
		public function setClubRelation(data :*):void
		{
			clubrelationList = data;
		}
		/**
		 *通过id获取俱乐部关系 
		 * @param id
		 * @return 
		 * 
		 */		
		public function getClubRelationById(id :int):*
		{
			for each(var data :* in clubrelationList)
			{
				if(data.npcid == id)
				{
					return data;
				}
			}
			return {};
		}
		/**
		 *球星是否已经签约 
		 * @param rlObj
		 * @param starid
		 * @return 
		 * 
		 */		
		public function isStarSign(rlObj :* ,starid:int):Boolean
		{
			if(rlObj==null)return false;
			return (rlObj.starid1==starid || rlObj.starid2==starid || rlObj.starid3==starid);
		}
		/**
		 *签约成功 
		 * @param npcid
		 * 
		 */		
		public function signSuccess(npcid :int,starid:int):void
		{
			var relObj :Object = getClubRelationById(npcid);
			relObj.relation = 0;
			for(var i :int = 1;i<=3;i++)
			{
				if(relObj.hasOwnProperty("starid"+i) && relObj["starid"+i]==0)
				{
					relObj["starid"+i] = starid;
					break;
				}
			}
			this.dispatchEvent(new Event(Event.CHANGE,true));
		}
	}
}